#! /bin/bash -e
#
# SPDX-License-Identifier: MIT
# SPDX-FileCopyrightText: (c) 2020-2023 Advanced Micro Devices, Inc.
#
##############################################################################
# This script is invoked by zf_mkdist on the dedicated zf build server.
##############################################################################

my_dir=$(cd $(dirname "$0") && /bin/pwd)
top_dir="${my_dir}/.."


locate_compiler() {
    compiler=$(readlink -f /opt/zf/cc || echo)
    if [ ! -e "${ZF_CC}" ]; then
        compiler="/opt/rh/devtoolset-7/root/usr/bin/cc"
    fi
    echo "${compiler}"
}

locate_onload_tree() {
    echo $(ls -d "${top_dir}"/../onload/*)
}


build_and_copy_artifacts() {
    build_type="$1"
    artifact_dir="$2"
    if [ "${build_type}" == "debug" ]; then
        echo "Building debug binaries"
        make_args=
    elif [ "${build_type}" == "shim" ]; then
        echo "Building socket shim"
        make_args="ZF_DEVEL=1 shim"
    else
        echo "Building ndebug binaries"
        make_args=NDEBUG=1
    fi

    build_dir="${top_dir}"/build
    rm -rf "${build_dir}"/onload
    rm -rf "${build_dir}"/gnu*
    cd "${top_dir}"
    make ${make_args}
    build_tree=$(ls -d "${build_dir}"/gnu*)
    copy_artifacts "${build_tree}" "${artifact_dir}" "${build_type}"
}


copy_artifacts() {
    build_dir="$1"
    dest_dir="$2"
    build_type="$3"

    if [ "${build_type}" == "shim" ]; then
        mkdir -p "${dest_dir}"/shim
        cp -a "$build_dir"/lib/libzf_sockets.so "${dest_dir}"/shim
        return
    fi

    lib_dir_stripped="${dest_dir}/stripped/${build_type}/lib"
    lib_dir_unstripped="${dest_dir}/unstripped/${build_type}/lib"
    bin_dir_stripped="${dest_dir}/stripped/${build_type}/bin"
    bin_dir_unstripped="${dest_dir}/unstripped/${build_type}/bin"
    mkdir -p "${lib_dir_stripped}"
    mkdir -p "${lib_dir_unstripped}"
    mkdir -p "${bin_dir_stripped}"
    mkdir -p "${bin_dir_unstripped}"

    # Copy stripped binaries
    cp -a "${build_dir}"/bin/stripped/zf_stackdump "${bin_dir_stripped}"
    cp -a "${build_dir}"/lib/stripped/libonload_zf_static.a "${lib_dir_stripped}"
    cp -a "${build_dir}"/lib/stripped/libonload_zf.so* "${lib_dir_stripped}"

    #Copy unstripped binaries
    cp -a "${build_dir}"/bin/zf_stackdump "${bin_dir_unstripped}"
    cp -a "${build_dir}"/lib/libonload_zf_static.a "${lib_dir_unstripped}"
    cp -a "${build_dir}"/lib/libonload_zf.so* "${lib_dir_unstripped}"
}

onload_tree=
shim=false
while [ $# -gt 0 ]; do
    case "$1" in
        --onload_tree)  shift; onload_tree="$1";;
        --shim) shim=true;;
        -*)  usage;;
    esac
    shift
done

if [ -z "$onload_tree" ]; then
    onload_tree=$(locate_onload_tree)
fi

if [ -z "$CC" ]; then
     export CC=$(locate_compiler)
fi


export ONLOAD_TREE=$onload_tree
artifact_dir="${top_dir}/build/artifacts/"
rm -rf ${artifact_dir}
build_and_copy_artifacts "debug" ${artifact_dir}
build_and_copy_artifacts "release" ${artifact_dir}
if [ "$shim" = "true" ]; then
    build_and_copy_artifacts "shim" "${artifact_dir}"
fi
